package sopia.sg;

import sopia.cmm.CommonFunct;
import sopia.conn.CategoryAdapter;
import sopia.conn.ExpenseAdapter;
import sopia.conn.ItemExpenseAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TabTypeExpenseActivity extends ListActivity {
	private ExpenseAdapter expAdapter ;
	private ItemExpenseAdapter itemAdapter ;
	private ListView lstTypes;
	private String[] names ;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initial();
	
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	  //Handle the back button
	  if (keyCode == KeyEvent.KEYCODE_BACK) {
		  Intent i = new Intent(TabTypeExpenseActivity.this, G001Activity.class);
			i.putExtra("ID", "G002");
			startActivity(i);
			finish();
	    return true;
	  }

	  return super.onKeyDown(keyCode, event);
	} 
	  private void initial() {
			this.names  = initListTypesExpense();
		    setListAdapter(new ArrayAdapter<String>(this, R.layout.g006_types_layout, names));
		    this.lstTypes = getListView();
		    lstTypes.setTextFilterEnabled(true);
		    registerForContextMenu(this.lstTypes);
	}
	@Override
	    public void onCreateContextMenu(ContextMenu menu, View v,
	        ContextMenuInfo menuInfo) {
		  if (v.getId()==android.R.id.list) {
			    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			    menu.setHeaderTitle(this.names[info.position]);
			    String[] menuItems = getResources().getStringArray(R.array.menu_view);
			    for (int i = 0; i<menuItems.length; i++) {
			      menu.add(Menu.NONE, i, i, menuItems[i]);
			    }
		}
	    }
	  @Override
	 public boolean onContextItemSelected(MenuItem item) {
		  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		  int menuItemIndex = item.getItemId();
		  final String nameExpense = this.names[info.position];
	      switch( menuItemIndex)
	      {
		      case 0: //delete	    
		    	  	new AlertDialog.Builder(this)
		    	    .setTitle(CommonFunct.join("Delete item ", nameExpense))
		    	    .setMessage("Are you sure you want to delete this item?")
		    	    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		    	        public void onClick(DialogInterface dialog, int which) { 
		    	            deleteTypesExpense(nameExpense);
		    	        }
		    	     })
		    	    .setNegativeButton("No", new DialogInterface.OnClickListener() {
		    	        public void onClick(DialogInterface dialog, int which) { 
		    	            // do nothing
		    	        }
		    	     })
		    	     .show();	    	  
		    	  break;
		      case 1: // show details
		    	  
		    	  break;
		      default:
		    	  
	      }
	    

	      return true;
	    }
    public void deleteTypesExpense(String nameExpense) {
			try {
				expAdapter = new ExpenseAdapter(this);
				if(expAdapter.deleteExpenseByAccountAndExpense(nameExpense,G001Activity.curNameAccount)){
					expAdapter.close();
					Toast.makeText(this,"Removed item selected." , Toast.LENGTH_LONG).show();
					deleteItemExpenseByAccount(nameExpense, G001Activity.curNameAccount);
					initial();   
				}	
			
			} catch (Exception e) {
				e.printStackTrace();
			}
		
	}
	public void deleteItemExpenseByAccount(String nameExpense, String curNameAccount) {
		try {
			itemAdapter = new ItemExpenseAdapter(this);
			if(itemAdapter.deleteItemExpenseByAccountAndExpense(nameExpense,curNameAccount)){
				itemAdapter.close();
				initial();   
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String[] initListTypesExpense() {
		String[] names = null ;
		try {
			expAdapter = new ExpenseAdapter(this);
			Cursor cur = expAdapter.getAllTypesExpense();
			names = new String[cur.getCount()];
			int i = 0 ;
			if (cur.moveToFirst()) {
			     do {
			    	 names[i++] = cur.getString(0);
			     } while (cur.moveToNext());
			 }
			if (cur != null && !cur.isClosed()) {
				  cur.close();
			}		
			expAdapter.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return names;
	}	
}
